package com.example.pojo;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * ClassName: Userfunction
 * Package: pojo
 * Description:
 *
 * @Author liu
 * @Create 2025/5/27 9:50
 * @ Version 1.0
 */
public class Userfunction {

    private static Scanner sc = new Scanner(System.in);
    private static List<User> students = new ArrayList<>();
    private static List<Dormitory> dormitories = new ArrayList<>();

    // 显示主菜单并获取用户选择
    public static int showMainMenu() {
        System.out.println("*********    宿舍管理系统 *********");
        System.out.println("*********    请选择：    *********");
        System.out.println("*********    1.管理员    *********");
        System.out.println("*********    2.学生      *********");
        System.out.println("*********    3.退出      *********");
        System.out.println();
        System.out.println("请输入：");
        return sc.nextInt();
    }

    // 用户登录方法
    public static User login() {
        System.out.println("请输入用户名：");
        User user = new User();
        String employeename = sc.next();
        user.setEmployeename(employeename);
        System.out.println("请输入密码：");
        String password = sc.next();
        user.setPassword(password);
        return user;
    }

    // 管理员功能菜单
    public static void adminFunctions(User user) {
        if ("admin".equals(user.getEmployeename()) && "123456".equals(user.getPassword())) {
            System.out.println("登录成功");
            System.out.println("*********    欢迎管理员：" + user.getEmployeename() + "    *********");
            boolean adminRunning = true;
            while (adminRunning) {
                System.out.println("*********    请选择：    *********");
                System.out.println("*********    1.查询所有学生信息    *********");
                System.out.println("*********    2.查询单个学生信息    *********");
                System.out.println("*********    3.添加学生信息    *********");
                System.out.println("*********    4.修改学生信息    *********");
                System.out.println("*********    5.删除学生信息    *********");
                System.out.println("*********    6.退出    *********");
                System.out.println("请输入：");
                int choice2 = sc.nextInt();
                switch (choice2) {
                    case 1:
                        if (students.isEmpty()) {
                            System.out.println("暂无学生信息");
                        } else {
                            for (User student : students) {
                                System.out.println(student);
                            }
                        }
                        break;
                    case 2:
                        System.out.println("请输入要查询的学生用户名：");
                        String queryName = sc.next();
                        boolean found = false;
                        for (User student : students) {
                            if (student.getEmployeename().equals(queryName)) {
                                System.out.println(student);
                                found = true;
                                break;
                            }
                        }
                        if (!found) {
                            System.out.println("未找到该学生信息");
                        }
                        break;
                    case 3:
                        addStudent();
                        break;
                    case 4:
                        System.out.println("请输入要修改的学生用户名：");
                        String modifyName = sc.next();
                        boolean modified = false;
                        for (User student : students) {
                            if (student.getEmployeename().equals(modifyName)) {
                                System.out.println("请输入新的学生密码：");
                                student.setPassword(sc.next());
                                System.out.println("学生信息修改成功");
                                modified = true;
                                break;
                            }
                        }
                        if (!modified) {
                            System.out.println("未找到该学生信息，无法修改");
                        }
                        break;
                    case 5:
                        System.out.println("请输入要删除的学生用户名：");
                        String deleteName = sc.next();
                        boolean deleted = false;
                        for (int i = 0; i < students.size(); i++) {
                            if (students.get(i).getEmployeename().equals(deleteName)) {
                                students.remove(i);
                                System.out.println("学生信息删除成功");
                                deleted = true;
                                break;
                            }
                        }
                        if (!deleted) {
                            System.out.println("未找到该学生信息，无法删除");
                        }
                        break;
                    case 6:
                        adminRunning = false;
                        System.out.println("退出成功");
                        break;
                    default:
                        System.out.println("输入的信息错误");
                        break;
                }
            }
        } else {
            System.out.println("用户名或密码错误");
        }
    }

    // 学生功能菜单
    public static void studentFunctions(User user) {
        System.out.println("登录成功");
        System.out.println("*********    欢迎学生：" + user.getEmployeename() + "    *********");
        boolean studentRunning = true;
        while (studentRunning) {
            System.out.println("*********    请选择：    *********");
            System.out.println("*********    1.查询宿舍信息    *********");
            System.out.println("*********    2.添加宿舍信息    *********");
            System.out.println("*********    3.修改宿舍信息    *********");
            System.out.println("*********    4.删除宿舍信息    *********");
            System.out.println("*********    5.退出           *********");
            System.out.println("请输入：");
            int choice2 = sc.nextInt();
            switch (choice2) {
                case 1:
                    if (dormitories.isEmpty()) {
                        System.out.println("暂无宿舍信息");
                    } else {
                        for (Dormitory dormitory : dormitories) {
                            System.out.println(dormitory);
                        }
                    }
                    break;
                case 2:
                    Dormitory newDormitory = new Dormitory();
                    System.out.println("请输入宿舍的栋数：");
                    newDormitory.setId(sc.next());
                    System.out.println("请输入宿舍名称：");
                    newDormitory.setName(sc.next());
                    dormitories.add(newDormitory);
                    System.out.println("宿舍信息添加成功");
                    break;
                case 3:
                    System.out.println("请输入要修改的宿舍编号：");
                    String modifyDormId = sc.next();
                    boolean dormModified = false;
                    for (Dormitory dormitory : dormitories) {
                        if (dormitory.getId().equals(modifyDormId)) {
                            System.out.println("请输入新的宿舍名称：");
                            dormitory.setName(sc.next());
                            System.out.println("宿舍信息修改成功");
                            dormModified = true;
                            break;
                        }
                    }
                    if (!dormModified) {
                        System.out.println("未找到该宿舍信息，无法修改");
                    }
                    break;
                case 4:
                    System.out.println("请输入要删除的宿舍编号：");
                    String deleteDormId = sc.next();
                    boolean dormDeleted = false;
                    for (int i = 0; i < dormitories.size(); i++) {
                        if (dormitories.get(i).getId().equals(deleteDormId)) {
                            dormitories.remove(i);
                            System.out.println("宿舍信息删除成功");
                            dormDeleted = true;
                            break;
                        }
                    }
                    if (!dormDeleted) {
                        System.out.println("未找到该宿舍信息，无法删除");
                    }
                    break;
                case 5:
                    studentRunning = false;
                    System.out.println("退出成功");
                    break;
                default:
                    System.out.println("输入的信息错误");
                    break;
            }
        }
    }
    public static void addStudent() {
        User newStudent = new User();
        System.out.println("请输入学生用户名：");
        newStudent.setEmployeename(sc.next());
        System.out.println("请输入学生密码：");
        newStudent.setPassword(sc.next());

        System.out.println("请输入学生所在宿舍编号：");
        String dormId = sc.next();
        Dormitory dormitory = findDormitoryById(dormId);
        if (dormitory != null) {
            newStudent.setDormitory(dormitory);
            students.add(newStudent);
            System.out.println("学生信息添加成功");
        } else {
            System.out.println("未找到该宿舍信息，学生信息添加失败");
        }
    }

    // 辅助方法：根据宿舍编号查找宿舍
    private static Dormitory findDormitoryById(String dormId) {
        for (Dormitory dormitory : dormitories) {
            if (dormitory.getId().equals(dormId)) {
                return dormitory;
            }
        }
        return null;
    }


    }
